/**
 * Created by howie on 2017/6/21.
 * npm install --save axios vue-axios
 */
import Vue from 'vue'
import VueAxios from 'vue-axios'
import axios from 'axios'
import router from '../router/index'
import utils from '../utils/user'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
var instance = axios.create({
  baseURL: 'http://10.8.220.6:8070/v1/',
  timeout: 5000,
})
//请求拦截器
instance.interceptors.request.use(config => {
  iView.LoadingBar.start()
  if (utils.isLogin()) {
    config.headers['Token'] = utils.getToken()
  }
  return config
}, err => {
  iView.LoadingBar.error()
  return Promise.reject(err);
});
//返回值拦截器
instance.interceptors.response.use(function (response) {
  if (response.status === 200) {
    iView.LoadingBar.finish()
    return response.data;
  } else {
    iView.LoadingBar.error()
    iView.Message.warning(response.data)
    return null
  }
}, function (error) {
  iView.LoadingBar.error()
  if (error.message === "Network Error") {
    iView.Message.error("未连接到服务器,请联系管理员")
    return null
  } else if (error.response.status === 333) {
    iView.Message.warning(error.response.data)
    utils.clearToken()
    router.push('/login')
  } else {
    iView.Message.warning(error.response.data)
  }
  return Promise.reject(error)
});

Vue.use(iView);
Vue.use(VueAxios, instance)
